package tosp.search;

import org.apache.log4j.Logger;

import tosp.HistoryMachine;
import tosp.JobSequence;
import tosp.job.Job;

public class ShakingNeighborSearch extends LocalSearchAdapter {

	static Logger log = Logger.getLogger(ShakingNeighborSearch.class);
	
	public ShakingNeighborSearch(HistoryMachine m) {
		super(m);
	}

	@Override
	public JobSequence[] getNeighbors(JobSequence j) {
		JobSequence[] ret=new JobSequence[j.jobs.length*15];
		int shakesize=Math.max(5, j.jobs.length/10);
		
		Job[] jobs=j.getJobsClone();
		
		for(int i=0;i<ret.length;i++) {
			//Job[] jobs=j.getJobsClone();
			for (int r=0;r<jobs.length*2/3;r++) {
				int from=(int)(Math.random()*jobs.length);
				int to=from+( (int)((Math.random()*shakesize*2)-shakesize));
				if (to>=jobs.length) to=jobs.length-1;
				if (to<0) to=0;
				if (from==to) continue;
				Job t=jobs[to];
				jobs[to]=jobs[from];
				jobs[from]=t;
			}
			JobSequence newjs = new JobSequence(jobs,true);
			log.trace("Generated new Neighbor: "+newjs.getSeqAsString());
			ret[i]=newjs;
		}
		log.debug("Generated a Total of "+ ret.length +" Neighbors");
		return ret;
	}

}
